//
//  NoticeViewController.m
//  Yu01
//
//  Created by 蒋松涵 on 16/8/19.
//  Copyright © 2016年 蒋松涵. All rights reserved.
//

#import "NoticeViewController.h"
#import "AFNetworking.h"
//af封装的一些关于ui方面的请求操作
#import "UIKit+AFNetworking.h"

//#import "NoticeModel.h"
#import "TongzhiViewController.h"

//
#import "SystemAnnouncement.h"


@interface NoticeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *_lineView;
    UIButton * button;
    
    
}
//滚动视图对象
@property (retain, nonatomic) UIScrollView *scrollView;
//表视图
@property (retain, nonatomic) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataArray;

@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;

@end

@implementation NoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    
    //导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //修改导航栏添加的按钮（item）颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    _urlStr = @"http://120.76.241.179/app/noticeInfo/listJson";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"page",@"10",@"rows",@"notice",@"noticeInfo. noticeTypeCode", nil];
    
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];
    
    self.title = @"通知";
    self.dataArray = [[NSMutableArray alloc]init];
    
//    [self scrolView];
//    [self _views];
    [self tableview];
//    [self afnetworking];
}

#pragma mark - 数据请求-----------
//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"++++++%@",dic);
        NSMutableArray * List = dic[@"rows"];
        for (NSDictionary * dicList in List)
        {
            SystemAnnouncement * model = [[SystemAnnouncement alloc]init];
            [model setValuesForKeysWithDictionary:dicList];
            [self.dataArray addObject:model];
            NSLog(@"000000%d",self.dataArray.count);
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
    }];
}






#pragma mark - View
-(void)_views
{
    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;

    
    
    UIView * _vi = [[UIView alloc]initWithFrame:CGRectMake(0, 64/h, ScreenWidth, 60/h)];
    [self.view addSubview:_vi];
    UIButton * buton = [UIButton buttonWithType:UIButtonTypeCustom];
    buton.frame = CGRectMake(ScreenWidth - 290/w, 15/h, 190/w, 60/h);
    [buton setTitle:@"关于我的重要通知" forState:UIControlStateNormal];
    [buton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    buton.titleLabel.font = [UIFont systemFontOfSize:17/w];
    [buton addTarget:self action:@selector(noticeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_vi addSubview:buton];
}

#pragma mark - ScrollView
-(void)scrolView
{
    //初始化scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 150)];
//    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    NSArray *array = [[NSArray alloc]initWithObjects:@"学校通知",@"教学通知",@"班级通知", nil];
    //    表示按钮宽度的和
    int sum = 0;
    //    设置按钮
    for (int i = 0; i < 3; i ++)
    {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
        //标题颜色 >>>高亮
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
        NSString *string = array[i];
        [button setTitle:string forState:UIControlStateNormal];
        
        //        string.length  字符串中字的个数
        //        NSLog(@"%f",string.length);
        
        //        font.pointSize 在字体大小Font下,每一个字的宽度
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        //        NSLog(@"%f------",button.titleLabel.font.pointSize);
        
        //        计算出按钮标题的宽度
        float width = string.length * button.titleLabel.font.pointSize;
        
        //        设置按钮位置
        button.frame = CGRectMake( sum, 0, width + 40, 40);
        //        记录按钮的总宽度
        sum = sum + width + 40;
        button.tag = i + 1;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:button];
    }
    self.scrollView.contentSize = CGSizeMake(sum, 40);
    
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(20, 38, 80, 2)];
    _lineView.backgroundColor = [UIColor orangeColor];
    [self.scrollView addSubview:_lineView];

}
#pragma mark - TableView
-(void)tableview
{
    CGFloat h = 736 / ScreenHeight;
//    CGFloat w = 414 / ScreenWidth;

    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64/h) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100/h;
    [self.view addSubview:self.tableView];
}

#pragma mark - 重要通知的点击方法
-(void)noticeBtnClick
{
    NSLog(@"你点击了");
}


#pragma mark - TableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
    NSLog(@"1234567890---%lu",(unsigned long)self.dataArray.count);
//    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;

    
    static NSString * CellIdentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    SystemAnnouncement * model = self.dataArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",model.logo]];
    
    UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 5, 150, 30)];
    label1.text = model.title;
    label1.font = [UIFont systemFontOfSize:14];
    [cell addSubview:label1];
    
    UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(120/w, 50/h, self.view.frame.size.width, 40/h)];
    label2.text = model.summary;
    label2.font = [UIFont systemFontOfSize:14];
    [cell addSubview:label2];
    
    
    UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width - 50, 30, 150, 30)];
    label3.text = model.createTime;
    label3.font = [UIFont systemFontOfSize:12];
    [cell addSubview:label3];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SystemAnnouncement * model = self.dataArray[indexPath.row];

        //
        self.hidesBottomBarWhenPushed = YES;
        TongzhiViewController * tongzhiVC = [[TongzhiViewController alloc]init];
    
    tongzhiVC.NotURL = [NSString stringWithFormat:@"http://www.100xsys.com.cn/school/app/noticeInfo/viewUI?noticeInfo.id=%@",model.id];
    
        [self.navigationController pushViewController:tongzhiVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
}

#pragma mark - Button的点击事件
//
- (void)btnClick:(UIButton *)sender
{
//    //    计算标题字体的长度
    float width = sender.titleLabel.text.length * sender.titleLabel.font.pointSize;
    //    设置下划线的位置及宽度
    [UIView animateWithDuration:0.6 animations:^{
        _lineView.frame = CGRectMake(sender.frame.origin.x + 20, 38, width, 2);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
